import { OutRequest } from './../domain/out-request';
import { ApiService } from './api.service';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
/*

@Author:赵明洋

@Date: 2018-12-12

@员工外出服务

@Last Modified time: 2018-12-16
*/

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
export class OutTest {
  user_id: number;
  start_time: string;
  end_time: string;
  reason: string;
  state: number; //审批状态
  duration: number; //请假时长
}

@Injectable({
  providedIn: 'root'
})


export class OutRequestStateService {
  private api_url;
  private headers;

  constructor(private apiService: ApiService,
    private http: HttpClient) 
    {
      this.api_url = apiService.getUrl();
    this.headers = apiService.getHeaders();
     }
           //获取请求信息
     getOutRequests (): Observable<OutRequest[]> {
      return this.http.get<OutRequest[]>(this.api_url + "/out_requests")
      .pipe(
        catchError(this.handleError('getOutRequests', []))
      );
    }
          //根据id获取单个请求信息
          getOutRequest(id:number): Observable<OutRequest[]>
          {
              return this.http.get<OutRequest[]>(this.api_url + "/out_requests"+"?id="+id)
              
          }  


    private handleError<T>(operation = 'operation', result?: T) {
      return (error: any): Observable<T> => {
  
        // TODO: send the error to remote logging infrastructure
        console.error(error); // log to console instead
  
        // TODO: better job of transforming error for user consumption
        console.log(`${operation} failed: ${error.message}`);
  
        // Let the app keep running by returning an empty result.
        return of(result as T);
      };
    }

/** POST: add a new hero to the database */
addOutRequest (outTest: OutTest): Observable<any> {
  return this.http.post(this.api_url + "/out_requests", outTest, this.headers)
    .pipe(
      catchError(this.handleError('addOutRequest', outTest))
    );
}

upDateoutRequest (outRequest: OutRequest): Observable<any> {
  return this.http.put(this.api_url + "/out_requests/"+outRequest.id, outRequest, this.headers)
    .pipe(
      catchError(this.handleError('upDateoutRequest', outRequest))
    );
}

deleteoutRequest (outRequest: OutRequest): Observable<any> {
  return this.http.delete(this.api_url + "/out_requests/"+outRequest.id, this.headers)
    .pipe(
      catchError(this.handleError('deleteoutRequest'))
    );
}


}
